package com.xdz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdz.constant.ProductConstant;
import com.xdz.dao.AttrDao;
import com.xdz.dao.AttrGroupDao;
import com.xdz.entity.AttrAttrgroupRelationEntity;
import com.xdz.entity.AttrEntity;
import com.xdz.entity.vo.AttrgroupRelationVo;
import com.xdz.entity.vo.GroupAndAttrsVo;
import com.xdz.service.AttrAttrgroupRelationService;
import com.xdz.service.AttrService;
import com.xdz.service.CategoryService;
import com.xdz.utils.PageUtils;
import com.xdz.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xdz.entity.AttrGroupEntity;
import com.xdz.service.AttrGroupService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCategoryId(Map<String, Object> params, Long categoryId) {
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq(AttrGroupEntity::getAttrGroupId, key).or()
                    .like(AttrGroupEntity::getAttrGroupName, key);
        }
        if(categoryId != 0) {
            wrapper.eq(AttrGroupEntity::getCatelogId, categoryId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getGroupAttr(Long id) {
        // 查询中间表
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, id));
        // 收集id
        List<Long> attrIds = relationEntities.stream().map((item -> {
            return item.getAttrId();
        })).collect(Collectors.toList());
        // 查询
        if(CollectionUtils.isEmpty(attrIds)){
            return null;
        }
        return attrService.listByIds(attrIds);
    }

    @Override
    public void attrRelationDelete(List<AttrgroupRelationVo> vos) {
        attrAttrgroupRelationService.deleteByAttrIdAndGroupId(vos);
    }

    @Override
    public PageUtils getGroupNoAttr(Map<String, Object> params, Long groupId) {
        // 查询当前所属分类
        AttrGroupEntity groupEntity = this.getById(groupId);
        Long catelogId = groupEntity.getCatelogId();
        // 查询该分类下其他组id
        List<AttrGroupEntity> attrGroupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>().select(AttrGroupEntity::getAttrGroupId).eq(AttrGroupEntity::getCatelogId, catelogId));
        // 找出其他组已经关联了的attrId,需要排除他们
        List<Long> ids = attrGroupEntities.stream().map((item) -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> attrgroupRelationEntities = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().in(AttrAttrgroupRelationEntity::getAttrGroupId, ids));
        // 找出同分类下其他组没有关联的基本属性
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getCatelogId, catelogId).eq(AttrEntity::getAttrType, ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        // 排除
        if(!CollectionUtils.isEmpty(attrgroupRelationEntities)){
            List<Long> noAttrIds = attrgroupRelationEntities.stream().map((item) -> {
                return item.getAttrId();
            }).collect(Collectors.toList());
            wrapper.and((w)->{
               w.notIn(AttrEntity::getAttrId,noAttrIds);
            });
        }
        // 条件搜索
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and((w)->{
                w.like(AttrEntity::getAttrId,key)
                        .or()
                        .like(AttrEntity::getAttrName,key);
            });
        }
        // 分页
        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public void addAttrRelation(List<AttrgroupRelationVo> vos) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = vos.stream().map((item) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationEntities);

    }

    @Override
    public List<GroupAndAttrsVo> getGroupAndAttrs(Long categoryId) {
        // 获取该分类下的属性分组
        List<AttrGroupEntity> groupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, categoryId));
        // 获取各分组下的属性
        List<GroupAndAttrsVo> vos = groupEntities.stream().map(item -> {
            GroupAndAttrsVo groupAndAttrsVo = new GroupAndAttrsVo();
            BeanUtils.copyProperties(item,groupAndAttrsVo);
            List<AttrEntity> attrList = this.getGroupAttr(item.getAttrGroupId());
            groupAndAttrsVo.setAttrs(attrList);
            return groupAndAttrsVo;
        }).collect(Collectors.toList());
        return vos;
    }
}